package com.ytjj.qmyx.data.util;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.utils.Aes128CBCUtil;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.data.model.ShortUrlBatchResponse;
import com.ytjj.qmyx.data.model.ShortUrlCommonResponse;
import org.springframework.util.CollectionUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

public class SmsErrorUtils {

    public static List<Object> readExcelFile() {
        String filePath = "C:\\Users\\86188\\Desktop\\pk_3.xlsx";
        File file=new File(filePath);
        if(!file.exists()){
            System.out.println("excel 文件不存在！");
        }
        Sheet sheet = new Sheet(1, 0, MobileVO.class);
        try {
            List<Object> list = EasyExcelFactory.read(new BufferedInputStream(new FileInputStream(file)), sheet);
            if (CollectionUtils.isEmpty(list)) {
                return null;
            }
            List<MobileVO> mobileVOList = new LinkedList<>();
            list.stream().forEach(item -> {
                MobileVO mobileVO = (MobileVO) item;
                if (null != mobileVO && !org.apache.commons.lang3.StringUtils.isEmpty(mobileVO.getMobile())) {
                    mobileVOList.add(mobileVO);
                }
            });
            return list;
        } catch (Exception e) {
            System.out.println(e) ;
        }
        return null;
    }

    public static void main(String[] args) {
        List<Object> list = readExcelFile();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<MobileVO> result = new ArrayList<>();
        list.stream().forEach(item -> {
            MobileVO mobileVO = (MobileVO) item;
            System.out.println(JSON.toJSONString(mobileVO));

            String content = mobileVO.getContent();
            if (StringUtils.isEmpty(content)) {
                content = "尊敬的客户您好，很抱歉给您带来不便，您在平台购买的商品因疫情原因暂时未能发出，如您愿意继续等待，疫情缓和后能发了会第一时间安排给您发出，如您不愿意等待麻烦您联系下平台客服给您处理售后：查询链接：https://vip.gucmall.cn//weapp/pages/order/orderlist.html?current=0 客服在线时间为9:30-21:00，感谢您的谅解。";
            }

            if (StringUtils.isEmpty(mobileVO.getMobile())) {
                return;
            }
            String[] mobiles = {mobileVO.getMobile()};
            String resultMsg = EncryptUtil.sendSanTong("http://www.dh3t.com/json/sms/Submit", "dh43329", "R9393fO9", mobiles, content, "【XXXX优品】");
            System.out.println("===============santong response==========" + resultMsg);
        });
        System.out.println("发完了");
        return;
    }

    public static String generateLongUrl(String longUrl, String phone, String orderSource) {
        String key = "eCcgc+=F'=M)$+'+";
        String iv = "g&_&J{~mK~e#h_Y2";
        String md5_phone = null;
        try {
            md5_phone = Aes128CBCUtil.encrypt(phone, key, iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (org.springframework.util.StringUtils.isEmpty(md5_phone)) {
            return null;
        }
        if (!longUrl.contains("?")) {
            longUrl = longUrl + "?serialNumber=";
        }else {
            longUrl = longUrl + "&serialNumber=";
        }
        md5_phone = md5_phone.replaceAll("\\+", "%2B");
        md5_phone = md5_phone.replaceAll("=", "%3D");
        md5_phone = md5_phone.replaceAll("/", "%2F");
        longUrl = longUrl + md5_phone;
        if (!StringUtils.isEmpty(orderSource)) {
            if ("android".equals(orderSource.toLowerCase())
                    || "ios".equals(orderSource.toLowerCase())
                    || "app".equals(orderSource.toLowerCase())) {
                longUrl = longUrl + "&orderSource=app";
            }else if (orderSource.startsWith("ISV") || orderSource.startsWith("weapp")) {
                longUrl = longUrl + "&orderSource=weapp";
            }else {
                longUrl = longUrl + "&orderSource=h5";
            }
        }
        return longUrl;
    }


    private static String generaterShortUrl(List<String> handleLongUrls) {
        long curTimeMillis = System.currentTimeMillis();
        System.out.println("====短链接口开始====" + curTimeMillis);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("urlArr", handleLongUrls);
        map.put("format", "json");
        map.put("domain", "z2m.cn");
        map.put("key", "6190c40b60f59d377d3b119566@9ce82b9d1ead5548a63205b58fd804e6");
        String result = HttpClientUtil.doPostJson("http://create.3wt.cn/batchCreate/tinyUrl", JSONObject.toJSONString(map));
        ShortUrlBatchResponse response = JSONObject.parseObject(result, ShortUrlBatchResponse.class);

        if (null == response) {
            System.out.println("===批量生成短链失败====返回对象为空了====");
            return null;
        }
        ShortUrlCommonResponse meta = response.getMeta();
        if (!"200".equals(meta.getCode())) {
            System.out.println("====批量生成短链失败===code:" + meta.getCode());
            return null;
        }
        List<Map<String, String>> data = response.getData();
        if (CollectionUtils.isEmpty(data)) {
            System.out.println("===批量生成短链返回data为空了=====");
            return null;
        }
        Map<String, String> dataMap = data.get(0);
        Collection<String> values = dataMap.values();
        AtomicReference<String> shortUrl = new AtomicReference<>("");
        values.stream().forEach(value -> {
            shortUrl.set(value);
        });
        if (!org.springframework.util.StringUtils.isEmpty(shortUrl.get())) {
            shortUrl.set(shortUrl.get().replace("http://",""));;
            shortUrl.set(shortUrl.get().replace("https://", ""));
        }
        return shortUrl.get();
    }
}
